package com.nlp.server.strategy;

import com.nlp.common.core.exception.ServiceException;
import com.nlp.server.constant.BaiDuConstant;
import com.nlp.server.pojo.dto.ChatDTO;
import com.nlp.server.service.ChatService;
import com.nlp.server.utils.BaiDuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * @author shiliuyinzhen
 */
@Component
public class SpeechQuery implements ChatQuery{

    @Autowired
    private ChatService chatService;

    @Autowired
    private BaiDuUtils baiDuUtils;

    /**
     * 语音识别查询问题
     * @param chatDTO
     * @return
     */
    @Override
    public void query(ChatDTO chatDTO,SseEmitter sseEmitter) {
        if (chatDTO.getSpeech() == null){
            throw new ServiceException("文件不能为空!");
        }
        if(chatDTO.getLen() == null ||chatDTO.getLen() <= 0){
            throw new ServiceException("文件大小必须大于0");
        }
        String question =
                baiDuUtils.getTextByShortVoice(BaiDuConstant.FORMAT_WAV,BaiDuConstant.RATE_DEFAULT
                ,BaiDuConstant.DEV_PID_CHINESE_DEFAULT,chatDTO.getLen(),chatDTO.getSpeech());
        chatDTO.setQuery(question);
        chatService.query(chatDTO,sseEmitter);
    }

}
